#lang racket
(require racket/draw racket/gui)

;PREDEFINED-------------------------------------------------------------------------------------------------------
(define frame (new frame% [label "Drawing Example"]
                   [width 800]
                   [height 600]))
(define canvas (new canvas% [parent frame]))
(define dc (send canvas get-dc))
(send frame show #t)
(sleep/yield 1)
;(send dc draw-line 0 0 30 30)
(define (draw-line p1 p2)
  (let ((x1 (xcor-vect p1))
        (y1 (ycor-vect p1))
        (x2 (xcor-vect p2))
        (y2 (ycor-vect p2)))
    (send dc draw-line x1 y1 x2 y2)))
;VECTOR-----------------------------------------------------------------------------------------------------------
(define (make-vect x y) (cons x y))

(define (xcor-vect v) (car v))

(define (ycor-vect v) (cdr v))

(define (add-vect v1 v2)
  (let ((x1 (car v1))
        (x2 (car v2))
        (y1 (cdr v1))
        (y2 (cdr v2)))
    (make-vect (+ x1 x2) (+ y1 y2))))

(define (sub-vect v1 v2)
  (let ((x1 (car v1))
        (x2 (car v2))
        (y1 (cdr v1))
        (y2 (cdr v2)))
    (make-vect (- x1 x2) (- y1 y2))))

(define (scale-vect s v)
  (let ((x (car v))
        (y (cdr v)))
    (make-vect (* x s) (* y s))))
;SEGMENT----------------------------------------------------------------------------------------------------------
(define (make-segment p1 p2) (cons p1 p2))

(define (start-segment seg) (car seg))

(define (end-segment seg) (cdr seg))
;FRAME-----------------------------------------------------------------------------------------------------------
(define (make-frame origin edge1 edge2)
  (cons origin (cons edge1 edge2)))

(define (origin-frame frame)
  (car frame))

(define (edge1-frame frame)
  (car (cdr frame)))

(define (edge2-frame frame)
  (cdr (cdr frame)))

(define (frame-coord-map frame)
  (lambda (v)
    (add-vect
     (origin-frame frame)
     (add-vect (scale-vect (xcor-vect v)
                           (edge1-frame frame))
               (scale-vect (ycor-vect v)
                           (edge2-frame frame))))))
;EXERCISE---------------------------------------------------------------------------------------------------------
(define (segments->painter segment-list)
  (lambda (frame)
    (for-each
     (lambda (segment)
       (draw-line
        ((frame-coord-map frame) (start-segment segment))
        ((frame-coord-map frame) (end-segment segment))))
     segment-list)))

(define 00p (make-vect 0 0))
(define 01p (make-vect 0 1))
(define 10p (make-vect 1 0))
(define 11p (make-vect 1 1))
(define r1 (make-vect 0.5 0))
(define r2 (make-vect 1 0.5))
(define r3 (make-vect 0.5 1))
(define r4 (make-vect 0 0.5))
(define just-frame (make-frame (make-vect 0 0) (make-vect 100 0) (make-vect 0 100)))
;a
(define square-list (list (make-segment 00p 01p)
                          (make-segment 01p 11p)
                          (make-segment 11p 10p)
                          (make-segment 10p 00p)))
;b
(define square-x-list (append square-list (list (make-segment 00p 11p)
                                                (make-segment 01p 10p))))
;c
(define rombhus-list (list (make-segment r1 r2)
                          (make-segment r2 r3)
                          (make-segment r3 r4)
                          (make-segment r4 r1)))

(define arrow-list (append square-list (list (make-segment (make-vect 0.5 0) (make-vect 0.5 1))
                                             (make-segment (make-vect 0.45 0.9) (make-vect 0.55 0.9)))))

((segments->painter arrow-list) just-frame)